import * as Cesium from 'cesium/Cesium'
import DistanceUnits from "./DistanceUnits";
import AreaUnits from "./AreaUnits";
import VolumeUnits from "./VolumeUnits";
import AngleUnits from "./AngleUnits";
function MeasureUnits(e) {
    e = Cesium.defaultValue(e, Cesium.defaultValue.EMPTY_OBJECT),
        this.distanceUnits = Cesium.defaultValue(e.distanceUnits, DistanceUnits.METERS),
        this.areaUnits = Cesium.defaultValue(e.areaUnits, AreaUnits.SQUARE_METERS),
        this.volumeUnits = Cesium.defaultValue(e.volumeUnits, VolumeUnits.CUBIC_METERS),
        this.angleUnits = Cesium.defaultValue(e.angleUnits, AngleUnits.DEGREES),
        this.slopeUnits = Cesium.defaultValue(e.slopeUnits, AngleUnits.DEGREES)
}

function getLocaleFormatStringOptions(e, t, i) {
    var r = {};
    return "number" == typeof (e = Cesium.defaultValue(e, 2)) ? r.maximumFractionDigits = r.minimumFractionDigits = e : r = "function" == typeof e ? e(t, i) : e, r
}

MeasureUnits.convertDistance = function (e, t, i) {
    return t === i ? e : e * getDistanceUnitConversion(t) * (1 / getDistanceUnitConversion(i))
}, MeasureUnits.convertArea = function (e, t, i) {
    return t === i ? e : e * getAreaUnitConversion(t) * (1 / getAreaUnitConversion(i))
}, MeasureUnits.convertVolume = function (e, t, i) {
    return t === i ? e : e * getVolumeUnitConversion(t) * (1 / getVolumeUnitConversion(i))
}, MeasureUnits.convertAngle = function (e, t, i) {
    return t === i ? e : convertAngleFromRadians(convertAngleToRadians(e, t), i)
}, MeasureUnits.numberToString = function (e, t, i) {
    return numberToFormattedString(e, t, i)
}, MeasureUnits.distanceToString = function (e, t, i, r) {
    return numberToFormattedString(MeasureUnits.convertDistance(e, DistanceUnits.METERS, t), i, r) + MeasureUnits.getDistanceUnitSpacing(t) + MeasureUnits.getDistanceUnitSymbol(t)
}, MeasureUnits.areaToString = function (e, t, i, r) {
    return numberToFormattedString(MeasureUnits.convertArea(e, AreaUnits.SQUARE_METERS, t), i, r) + MeasureUnits.getAreaUnitSpacing(t) + MeasureUnits.getAreaUnitSymbol(t)
}, MeasureUnits.volumeToString = function (e, t, i, r) {
    return numberToFormattedString(MeasureUnits.convertVolume(e, VolumeUnits.CUBIC_METERS, t), i, r) + MeasureUnits.getVolumeUnitSpacing(t) + MeasureUnits.getVolumeUnitSymbol(t)
}, MeasureUnits.angleToString = function (e, t, i, r) {
    if (t === AngleUnits.DEGREES || t === AngleUnits.RADIANS || t === AngleUnits.GRADE) return numberToFormattedString(convertAngleFromRadians(e, t), i, r) + MeasureUnits.getAngleUnitSpacing(t) + MeasureUnits.getAngleUnitSymbol(t);
    if (t === AngleUnits.DEGREES_MINUTES_SECONDS) {
        var n = Cesium.Math.toDegrees(e), a = n < 0 ? "-" : "";
        n = Math.abs(n);
        var o = Math.floor(n), s = 60 * (n - o), l = Math.floor(s), c = 60 * (s - l);
        return a + o + "° " + l + "' " + (c = numberToFormattedString(c, void 0, r)) + '"'
    }
    if (t === AngleUnits.RATIO) {
        var u = 1 / convertAngleFromRadians(e, t), d = getLocaleFormatStringOptions(r);
        return d.minimumFractionDigits = 0, "1:" + numberToFormattedString(u, i, d)
    }
}, MeasureUnits.longitudeToString = function (e, t, i, r) {
    return MeasureUnits.angleToString(Math.abs(e), t, i, r) + " " + (e < 0 ? "W" : "E")
}, MeasureUnits.latitudeToString = function (e, t, i, r) {
    return MeasureUnits.angleToString(Math.abs(e), t, i, r) + " " + (e < 0 ? "S" : "N")
}, MeasureUnits.getDistanceUnitSymbol = function (e) {
    return e === DistanceUnits.METERS ? "m" : e === DistanceUnits.CENTIMETERS ? "cm" : e === DistanceUnits.KILOMETERS ? "km" : e === DistanceUnits.FEET || e === DistanceUnits.US_SURVEY_FEET ? "ft" : e === DistanceUnits.INCHES ? "in" : e === DistanceUnits.YARDS ? "yd" : e === DistanceUnits.MILES ? "mi" : void 0
}, MeasureUnits.getDistanceUnitSpacing = function (e) {
    return " "
}, MeasureUnits.getAreaUnitSymbol = function (e) {
    return e === AreaUnits.SQUARE_METERS ? "m²" : e === AreaUnits.SQUARE_CENTIMETERS ? "cm²" : e === AreaUnits.SQUARE_KILOMETERS ? "km²" : e === AreaUnits.SQUARE_FEET ? "sq ft" : e === AreaUnits.SQUARE_INCHES ? "sq in" : e === AreaUnits.SQUARE_YARDS ? "sq yd" : e === AreaUnits.SQUARE_MILES ? "sq mi" : e === AreaUnits.ACRES ? "ac" : e === AreaUnits.HECTARES ? "ha" : void 0
}, MeasureUnits.getAreaUnitSpacing = function (e) {
    return " "
}, MeasureUnits.getVolumeUnitSymbol = function (e) {
    return e === VolumeUnits.CUBIC_METERS ? "m³" : e === VolumeUnits.CUBIC_CENTIMETERS ? "cm³" : e === VolumeUnits.CUBIC_KILOMETERS ? "km³" : e === VolumeUnits.CUBIC_FEET ? "cu ft" : e === VolumeUnits.CUBIC_INCHES ? "cu in" : e === VolumeUnits.CUBIC_YARDS ? "cu yd" : e === VolumeUnits.CUBIC_MILES ? "cu mi" : void 0
}, MeasureUnits.getVolumeUnitSpacing = function (e) {
    return " "
}, MeasureUnits.getAngleUnitSymbol = function (e) {
    return e === AngleUnits.DEGREES ? "°" : e === AngleUnits.RADIANS ? "rad" : e === AngleUnits.GRADE ? "%" : void 0
}, MeasureUnits.getAngleUnitSpacing = function (e) {
    return e === AngleUnits.RADIANS ? " " : ""
};
var negativeZero = -0, positiveZero = 0;

function numberToFormattedString(e, t, i) {
    var r = getLocaleFormatStringOptions(i, e, t), n = e.toLocaleString(t, r);
    return n === negativeZero.toLocaleString(t, r) ? positiveZero.toLocaleString(t, r) : n
}

function getDistanceUnitConversion(e) {
    return e === DistanceUnits.METERS ? 1 : e === DistanceUnits.CENTIMETERS ? .01 : e === DistanceUnits.KILOMETERS ? 1e3 : e === DistanceUnits.FEET ? .3048 : e === DistanceUnits.US_SURVEY_FEET ? 1200 / 3937 : e === DistanceUnits.INCHES ? .0254 : e === DistanceUnits.YARDS ? .9144 : e === DistanceUnits.MILES ? 1609.344 : void 0
}

function getAreaUnitConversion(e) {
    return e === AreaUnits.SQUARE_METERS ? 1 : e === AreaUnits.SQUARE_CENTIMETERS ? 1e-4 : e === AreaUnits.SQUARE_KILOMETERS ? 1e6 : e === AreaUnits.SQUARE_FEET ? .09290304 : e === AreaUnits.SQUARE_INCHES ? 64516e-8 : e === AreaUnits.SQUARE_YARDS ? .83612736 : e === AreaUnits.SQUARE_MILES ? 2589988.110336 : e === AreaUnits.ACRES ? 4046.85642232 : e === AreaUnits.HECTARES ? 1e4 : void 0
}

function getVolumeUnitConversion(e) {
    return e === VolumeUnits.CUBIC_METERS ? 1 : e === VolumeUnits.CUBIC_CENTIMETERS ? 1e-6 : e === VolumeUnits.CUBIC_KILOMETERS ? 1e9 : e === VolumeUnits.CUBIC_FEET ? .09290304 * .3048 : e === VolumeUnits.CUBIC_INCHES ? 16387064e-12 : e === VolumeUnits.CUBIC_YARDS ? .764554857984 : e === VolumeUnits.CUBIC_MILES ? 4168181825.44058 : void 0
}

var degreesMinutesSecondsRegex = /(-?)(\d+)\s*°\s*(\d+)\s*'\s*([\d.,]+)"\s*([WENS]?)/i;

function convertAngleToRadians(e, t) {
    if (t === AngleUnits.RADIANS) return e;
    if (t === AngleUnits.DEGREES) return Cesium.Math.toRadians(e);
    if (t === AngleUnits.GRADE) return e === Number.POSITIVE_INFINITY ? Cesium.Math.PI_OVER_TWO : Math.atan(e / 100);
    if (t === AngleUnits.RATIO) return Math.atan(e);
    if (t === AngleUnits.DEGREES_MINUTES_SECONDS) {
        var i = degreesMinutesSecondsRegex.exec(e);
        if (!Cesium.defined(i)) throw new RuntimeError("Could not convert angle to radians: " + e);
        var r = 0 < i[1].length ? -1 : 1, n = parseInt(i[2]), a = parseInt(i[3]), o = parseFloat(i[4]), s = i[5];
        1 === s.length && ("W" !== (s = s.toUpperCase()) && "S" !== s || (r *= -1));
        var l = r * (n + a / 60 + o / 3600);
        return Cesium.Math.toRadians(l)
    }
}

function convertAngleFromRadians(e, t) {
    return t === AngleUnits.RADIANS ? e : t === AngleUnits.DEGREES ? Cesium.Math.toDegrees(e) : t === AngleUnits.GRADE ? (e = Cesium.Math.clamp(e, 0, Cesium.Math.PI_OVER_TWO)) === Cesium.Math.PI_OVER_TWO ? Number.POSITIVE_INFINITY : 100 * Math.tan(e) : t === AngleUnits.RATIO ? Math.sin(e) / Math.cos(e) : void 0
}



export default  MeasureUnits;